// 商家销售统计的横向柱状图
<template>
  <div class="com_container">
    <div class="title" :style="comStyle">
      <span :style="titleStyle" class="title_text" @click="showSelect = !showSelect">{{ "▎" + currentTitle }}</span>
      <!-- 箭头 -->
      <span class="iconfont title_text" :style="comStyle" @click="showSelect = !showSelect">&#xe6eb;</span>
      <div class="select_title title_text" :style="marginStyle" v-show="showSelect">
        <div
          v-for="item in showTitle"
          :key="item.key"
          @click="currentType(item.key)"
          :style="titleStyle"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="com_chart" ref="seller_page"></div>
  </div>
</template>

<script>
import { getThemeValue } from '@/utils/theme_utils';
import { mapState } from 'vuex'
export default {
  name: "Trend",
  data() {
    return {
      dom: null, // 挂载图表容器的实例对象
      data: null, // 服务器中获取的数据
      titleAdaptData: 0, // 标题适配的数据
      showSelect: false, // true显示，false不显示
      dataType: "map", // 数据的类型(根据类型显示数据)
    };
  },
  created() {
    // 在组件创建完成之后  进行回调函数的注册
    this.$socket.registerCallBack('trendData', this.getData);
  },
  // 模板渲染成html后调用
  mounted() {
    this.initChart();
    // 发送数据给服务器，告诉服务器，我现在需要数据
    this.$socket.send({
      action: "getData",
      socketType: "trendData",
      chartName: "trend",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapt);
    this.screenAdapt();
  },
  // 组件销毁后
  destroyed() {
    window.removeEventListener("resize", this.screenAdapt); // 移除这个事件
    // 在组件销毁的时候，进行回调函数的取消
    this.$socket.unRegisterCallBack("trendData");
  },
  methods: {
    // 初始化echart实例(lnstance)对象
    initChart() {
      this.dom = this.$echarts.init(this.$refs.seller_page, this.theme);
      // this.getData();
    },
    // 获取服务器的数据
    getData(data) {
      this.data = data;
      this.updateChart();
    },
    // 更新图表视图
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = [
        "rgba(11, 168, 44, 0.5)",
        "rgba(44, 110, 255, 0.5)",
        "rgba(22, 242, 217, 0.5)",
        "rgba(254, 33, 30, 0.5)",
        "rgba(250, 105, 0, 0.5)",
      ];
      // 全透明的颜色值
      const colorArr2 = [
        "rgba(11, 168, 44, 0)",
        "rgba(44, 110, 255, 0)",
        "rgba(22, 242, 217, 0)",
        "rgba(254, 33, 30, 0)",
        "rgba(250, 105, 0, 0)",
      ];

      // x轴数据
      let timerValue = this.data.common.month;
      // y轴数据
      let valueArr = this.data[this.dataType].data;
      let seriesArr = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: this.dataType, // 推叠图效果
          // 第一种
          // areaStyle: {
          //   color: {
          //     type: 'linear',
          //     x: 0,
          //     y: 0,
          //     x2: 0,
          //     y2: 1,
          //     colorStops: [
          //       {
          //         offset: 0,
          //         color: colorArr1[index]
          //       },
          //       {
          //         offset: 1,
          //         color: colorArr2[index]
          //       }
          //     ]
          //   }
          // },
          // 第二种
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              },
              {
                offset: 1,
                color: colorArr2[index],
              },
            ]),
          },
        };
      });
      // 图例的数据
      let legendArr = valueArr.map((item) => {
        return item.name;
      });
      // 配置项
      let option = {
        xAxis: {
          data: timerValue,
          // 紧挨边缘
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
        },
        tooltip: {
          trigger: "axis",
        },
        grid: {
          left: 20,
          top: "20%",
          right: 30,
          containLabel: true,
        },
        legend: {
          left: 50,
          top: "12.5%",
          icon: "circle", // 图例项的 icon
          data: legendArr,
        },
        series: seriesArr,
      };
      this.dom.setOption(option);
    },
    // 当浏览器大小发生变化时，就要让图表适配屏幕
    screenAdapt() {
      this.titleAdaptData = (this.$refs.seller_page.offsetWidth / 100) * 3.6;
      this.dom.resize();
      let option = {
        legend: {
          itemGap: this.titleAdaptData,
          itemWidth: this.titleAdaptData,
          itemHeight: this.titleAdaptData,
          textStyle: {
            fontSize: this.titleAdaptData / 1.3,
          },
        },
      };
      this.dom.setOption(option);
    },
    currentType(type) {
      this.dataType = type;
      this.showSelect = false;
      this.updateChart();
    },
  },
  computed: {
    // 显示标题的数组数据
    showTitle() {
      if (!this.data) {
        return [];
      } else {
        return this.data.type.filter((item) => {
          return item.key != this.dataType;
        });
      }
    },
    currentTitle() {
      if (!this.data) {
        return "";
      } else {
        return this.data[this.dataType].title;
      }
    },
    // 给标题添加的样式
    comStyle() {
      return {
        fontSize: this.titleAdaptData * 1.3 + "px",
        color: getThemeValue(this.theme).titleColor
      };
    },
    marginStyle() {
      return {
        marginLeft: this.titleAdaptData / 2 + "px",
      };
    },
    ...mapState(['theme']),
    titleStyle() {
      return {
        color: getThemeValue(this.theme).titleColor
      }
    }
  },
  watch: {
    theme() {
      this.dom.dispose() // 销毁当前的图表
      this.initChart()  // 重新以最新的主题名称初始化图表对象
      this.screenAdapt()  // 完成屏幕的适配
      this.updateChart()  // 更新图表的展示
    }
  }
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  top: 20px;
  left: 25px;
  z-index: 10;
  color: #fff;
  cursor: pointer;

  .title_text{
    position: relative;
    top: 3px;
    font-weight:900;
  }

  .iconfont {
    cursor: pointer;
  }

  .select_title div {
    cursor: pointer;
  }

  .select_title {
    border-radius: 20px;
  }
}
</style>
